<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin', 'admin', 0);
pc_base::load_sys_class('format', '', 0);
pc_base::load_sys_class('form', '', 0);

class album extends admin
{

    private $db;

    public $site;

    public $type;
    public $video;

    function __construct()
    {
        parent::__construct();
        pc_base::load_app_func('global');
        $this->db = pc_base::load_model('album_model'); // 加载model
        $this->siteid = get_siteid(); // 获取当前站点
        $this->picture = pc_base::load_model('picture_nsr_model');
        $this->type = pc_base::load_model('source_type_model');
        $this->admin_username = param::get_cookie('admin_username'); // 获取当前登录的管理员
    }

//列表
    public function init()
    {
        if (! isset($_REQUEST['talent_id']) || empty($_REQUEST['talent_id'])) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $id=$talent_id = $_REQUEST['talent_id'];
        $model_talent = pc_base::load_model('talent_model'); // 加载model
        $data = $model_talent->get_one(array(
            'id' => $id
        ));
        if($source_type==1){
        	$table="nsrweb_news_nsr";
        	$btn_info="资讯";
        }elseif($source_type==2){
        	$table="nsrweb_video_nsr";
        	$btn_info="视频";
        }elseif($source_type==3){
        	$table="nsrweb_picture_nsr";
        	$btn_info="图集";
        }

        $where = "";
        $show_dialog = $show_validator = true;
        $show_header = '';
        $where .= "status = 1 and talent_id=".$talent_id." and siteid =" . $this->siteid;


        // 搜索框
        $keyword = isset($_GET['keyword']) ? $_GET['keyword'] : '';

        if (isset($_GET['search'])) {
            if ($keyword) {
                    $where .= " and `name` like '%$keyword%'";
            }
        }

        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $talentinfo = $this->db->listinfo($where, 'id DESC', $page, 10);
        $pages = $this->db->pages;

        include $this->admin_tpl('album_list');
    }

    /**
     * 添加
     */
    public function add()
    {

        if (isset($_POST['dosubmit'])) {

            $data = isset($_POST['data']) ? $_POST['data'] : showmessage(L('illegal_parameters'), HTTP_REFERER);

            $data['created_by'] =  $_SESSION['userid'];
            $data['updated_by'] =  $_SESSION['userid'];

            $data['create_time'] = date("Y-m-d H:i:s",time());
            $data['update_time'] = date("Y-m-d H:i:s",time());
            $data['siteid'] = $this->siteid;
            $data['status'] = 1;
            $result = $this->db->insert($data);
            if ($result) {
                showmessage(L('add_success'), '?m=resource_asso&c=album&a=init&talent_id='.$data['talent_id'],'','add');
            } else {
                showmessage(L('add_error'), HTTP_REFERER);
            }
        } else {
            $show_dialog = $show_validator = true;
            $show_header = '';
            if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
                showmessage(L('illegal_action'), HTTP_REFERER);
            }
            $id = $_REQUEST['id'];
            pc_base::load_sys_class('format', '', 0);
            pc_base::load_sys_class('form', '', 0);
            include $this->admin_tpl('album_add');
        }
    }

    /**
     * 编辑
     */
    public function edit()
    {
        $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : showmessage(L('illegal_parameters'), HTTP_REFERER);
        $data = $this->db->get_one(array(
            'id' => $id
        ));
        $upinfo = $_POST['data'];
        if (isset($_POST['dosubmit'])) {

            $data = isset($_POST['data']) ? $_POST['data'] : showmessage(L('illegal_parameters'), HTTP_REFERER);

            $data['updated_by'] =  $_SESSION['userid'];

            $data['update_time'] = date("Y-m-d H:i:s",time());
            $data['siteid'] = $this->siteid;
            $data['status'] = 1;
            $result = $this->db->update($data, array(
                'id' => $id
            ));
            if ($result) {
                showmessage(L('edit_success'), '?m=resource_asso&c=album&a=init&talent_id='.$data['talent_id'],'','edit');
            } else {
                showmessage(L('edit_error'), HTTP_REFERER);
            }
        } else {
            //$show_dialog = $show_validator = true;
            $show_dialog = $show_validator = true;
            $show_header = '';

            include $this->admin_tpl('album_edit');
        }
    }

    /**
     * 删除、批量删除
     */
    public function del()
    {
        if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $id = $_REQUEST['id'];
        if (is_array($id)) {
            foreach ($id as $k => $v) {
                if (intval($v)) {
                    $id[$k] = intval($v);
                } else {
                    unset($id[$k]);
                }
            }
            $id = implode('\',\'', $id);
        }
        $result = $this->db->update(array(
            'status' => '2'
        ), "id in ('$id')");
        if ($result) {
            showmessage(L('del_success'), HTTP_REFERER);
        } else {
            showmessage(L('del_error'), HTTP_REFERER);
        }
    }

    /*详情*/
    public function view()
    {
        if (! isset($_REQUEST['talent_id']) || empty($_REQUEST['talent_id'])) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $source_type=isset($_REQUEST['source_type'])?$_REQUEST['source_type']:1;


        $table="nsrweb_picture_nsr";

        $id = $_REQUEST['id'];
        $talent_id = $_REQUEST['talent_id'];

        $model_talent = pc_base::load_model('talent_model'); // 加载model
        $data = $model_talent->get_one(array(
            'id' => $talent_id
        ));
        $show_dialog = $show_validator = true;
        $show_header = '';

        $sql="SELECT n.*,c.id as cid  FROM nsrweb_album_picture AS c LEFT JOIN ".$table." AS n ON c.picture_id = n.id WHERE n.`status` = 1";

        // 搜索框
        $title = isset($_GET['title']) ? $_GET['title'] : '';

        if (isset($_GET['search'])) {
            if ($title) {
                $sql .= " and n.`title` LIKE '%$title%'";
            }
        }

        $sql.=" AND c.album_id=".$id."  AND n.siteid = ".$this->siteid." ORDER BY  c.update_time desc";

        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $sourceinfo = $this->db->my_listinfo($sql, $page, 10);
        $pages = $this->db->pages;
        include $this->admin_tpl('album_picture_list');
    }


    /**
     * 添加
     */
    public function addPicture()
    {

        if (isset($_POST['dosubmit'])) {




            $model=$this->picture;
            $information =$model->check($_POST['picture']);

            $id = $model->edit($information);
            //添加缓存
            // $this->news->redisCache($id);
            if ($id) {
                $data=array();

                $data['picture_id']=$id;
                $data['album_id']=$_POST['album_id'];
                $data['created_by'] =  $_SESSION['userid'];
                $data['updated_by'] =  $_SESSION['userid'];

                $data['create_time'] = date("Y-m-d H:i:s",time());
                $data['update_time'] = date("Y-m-d H:i:s",time());
                $collect=pc_base::load_model('album_picture_model'); // 加载model;
                $res = $collect->insert($data);
                if ($res) {
                    showmessage(L('add_success'), '?m=resource_asso&c=album&a=view&talent_id='.$_POST['talnet_id'].'&id='.$_POST['album_id'],'','add');
                } else {
                    showmessage(L('add_error'), HTTP_REFERER);
                }
            } else {
                showmessage(L('add_information_error'), HTTP_REFERER);
            }
        } else {
            $show_dialog = $show_validator = true;
            $show_header = '';
            $types = $this->type->getSource(3);

            if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
                showmessage(L('illegal_action'), HTTP_REFERER);
            }
            $id= $_REQUEST['id'];
            $talent_id= $_REQUEST['talent_id'];
            pc_base::load_sys_class('format', '', 0);
            pc_base::load_sys_class('form', '', 0);

            $action_source="talent_album";
            include $this->admin_tpl('picture_add','picture');


        }
    }

    /**
     * 批量删除
     */
    public function deleteAll ()
    {
        $picture=pc_base::load_model('album_picture_model'); // 加载model;

        if(isset($_GET['dosubmit'])) {
            $id = implode('\',\'', $_POST['ids']);

            $result =$picture->delete("id in ('$id')");
            if ($result) {
                showmessage(L('del_success'), HTTP_REFERER);
            } else {
                showmessage(L('del_error'), HTTP_REFERER);
            }
        } else {
            showmessage(L('operation_failure'));
        }
    }

    /**
     * 删除、批量删除
     */
    public function delPicture()
    {
        $picture=pc_base::load_model('album_picture_model'); // 加载model;
        if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $id = $_REQUEST['id'];
        if (is_array($id)) {
            foreach ($id as $k => $v) {
                if (intval($v)) {
                    $id[$k] = intval($v);
                } else {
                    unset($id[$k]);
                }
            }
            $id = implode('\',\'', $id);
        }
        $result = $picture->delete("id in ('$id')");


        if ($result) {
            showmessage(L('del_success'), HTTP_REFERER);
        } else {
            showmessage(L('del_error'), HTTP_REFERER);
        }
    }



    public function bindView(){

        $picture=pc_base::load_model('album_picture_model'); // 加载model;
        $id = $_REQUEST['id'];
        $source_array=$picture->select(array("album_id"=>$id),"picture_id");

        $where=" status=1 and  siteid =" . $this->siteid;

        // 搜索框
        $keywords = isset($_GET['keywords']) ? $_GET['keywords'] : '';

        if (isset($_GET['search'])) {
            if ($keywords) {
                $where .= " and `title` LIKE '%$keywords%'";
            }
        }

        if(!empty($source_array)){
            foreach ($source_array as $k => $v) {
                $sid[] = intval($v['picture_id']);
            }
            $sid = implode('\',\'', $sid);
            $where.=" and id not in ('$sid')";
        }


        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $model=$this->picture;

        $show_dialog = $show_validator = true;
        $show_header = '';
        $infos=$model->listinfo($where,'id DESC',$page,10);
        $pages = $model->pages;
        include $this->admin_tpl('picture_select','picture');




    }


    public function  bindAction(){
        $id= $_REQUEST['id'];
        $relation=$_REQUEST['relation'];
        $source=explode("|",$relation);
        $data=array();

        $collect=pc_base::load_model('album_picture_model'); // 加载model;

        foreach($source as $k=>$v){

            $data[$k]['album_id']=$id;

            $data[$k]['picture_id']=$v;

            $data[$k]['created_by'] =  $_SESSION['userid'];
            $data[$k]['updated_by'] =  $_SESSION['userid'];

            $data[$k]['create_time'] = date("Y-m-d H:i:s",time());
            $data[$k]['update_time'] = date("Y-m-d H:i:s",time());
        }
        $res=$collect->multiInsert($data);
        if($res){
            echo  json_encode(array("status"=>true,"code"=>200,"msg"=>"选择成功"));
        }else{
            echo  json_encode(array("status"=>false,"code"=>1000,"msg"=>"选择失败"));
        }
    }

}